package com.wendy.system.conf.controller;

import com.wendy.common.core.domain.TableDataInfo;
import com.wendy.common.log.annotation.Log;
import com.wendy.common.log.enums.BusinessType;
import com.wendy.common.web.controller.BaseController;
import com.wendy.custom.security.annotation.RequiresPermissions;
import com.wendy.custom.security.utils.SecurityUtils;
import com.wendy.excel.ruoyi.utils.ExcelUtil;
import com.wendy.system.conf.domain.bo.SysConfigBo;
import com.wendy.system.conf.domain.vo.SysConfigExcelVo;
import com.wendy.system.conf.domain.vo.SysConfigVo;
import com.wendy.system.conf.entity.SysConfig;
import com.wendy.system.conf.errorcode.SysConfigError;
import com.wendy.system.conf.service.ISysConfigService;
import com.wendy.utils.spring.MapstructUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 参数配置 信息操作处理
 *
 * @author wendy
 */
@RestController
@RequestMapping("/config")
public class SysConfigController extends BaseController {
    @Autowired
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @RequiresPermissions("system:config:list")
    @GetMapping("/page")
    public TableDataInfo page(SysConfigBo config) {
        TableDataInfo<SysConfigVo> list = configService.selectConfigPage(config);
        return list;
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:config:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfigBo config) {
        List<SysConfig> list = configService.selectConfigList(config);
        ExcelUtil<SysConfigExcelVo> util = new ExcelUtil<SysConfigExcelVo>(SysConfigExcelVo.class);
        List<SysConfigExcelVo> result = MapstructUtils.convert(list, SysConfigExcelVo.class);
        util.exportExcel(response, result, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/{configId}")
    public SysConfigVo getInfo(@PathVariable Long configId) {
        return MapstructUtils.convert(configService.selectConfigById(configId), SysConfigVo.class);
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public String getConfigKey(@PathVariable String configKey) {
        return configService.selectConfigByKey(configKey);
    }

    /**
     * 新增参数配置
     */
    @RequiresPermissions("system:config:add")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public int add(@Validated @RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            SysConfigError.ADD_ERROR.formatFire(config.getConfigName());
        }
        config.setCreateBy(SecurityUtils.getUsername());
        return configService.insertConfig(config);
    }

    /**
     * 修改参数配置
     */
    @RequiresPermissions("system:config:edit")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public int edit(@Validated @RequestBody SysConfig config) {
        if (!configService.checkConfigKeyUnique(config)) {
            SysConfigError.EDIT_ERROR.formatFire(config.getConfigName());
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        return configService.updateConfig(config);
    }

    /**
     * 删除参数配置
     */
    @RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public boolean remove(@PathVariable Long[] configIds) {
        configService.deleteConfigByIds(configIds);
        return true;
    }

    /**
     * 刷新参数缓存
     */
    @RequiresPermissions("system:config:remove")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public boolean refreshCache() {
        configService.resetConfigCache();
        return true;
    }
}
